package service

import (
	"context"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/config"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/userwash"
	schemaUserWash "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/userwash"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/settingtoc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userservice"
	"github.com/bytedance/sonic"
	"github.com/zeromicro/go-zero/core/logc"
	"gorm.io/gorm"
)

type UserWashService struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserWashService(ctx context.Context, svcCtx *svc.ServiceContext) *UserWashService {
	return &UserWashService{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (w *UserWashService) IsOpenWash() int32 {
	resp, err := w.svcCtx.SettingRpc.DataView(w.ctx, &settingtoc.DataViewReq{
		MerchantId: 1,
		ModelType:  1,
		Type:       10011,
	})
	if err != nil {
		logc.Error(w.ctx, "SettingRpc.Settingtoc.DataView,err", err)
		return 0
	}
	var m map[string]int
	if err = sonic.UnmarshalString(resp.Data.List[0].Value, &m); err != nil {
		logc.Error(w.ctx, "sonic.UnmarshalString,err", err)
		return 0
	}
	status := int32(m["status"])
	return status
}

func (w *UserWashService) DoUserWash(userId int64, typeName string) error {
	userWashBetList, err := userwash.NewUserWashBetModel(w.ctx, w.svcCtx.Db).GetUserWashBet(userId, typeName)
	if err != nil {
		return err
	}
	vipInfo, err := w.svcCtx.UserRpc.UserGetVipInfo(w.ctx, &userservice.UserGetVIPLevelByIdRequest{UserId: userId})
	if err != nil {
		return err
	}
	// 进行洗码
	for _, userWashBet := range userWashBetList {
		for _, rebateRate := range vipInfo.RebateRateList {
			if userWashBet.TypeName == rebateRate.TypeName {
				uw := &schemaUserWash.UserWash{
					UserId:       userWashBet.UserId,
					TypeName:     userWashBet.TypeName,
					CategoryCode: userWashBet.CategoryCode,
					Rate:         rebateRate.Value,
					Bet:          userWashBet.Value,
					Value:        userWashBet.Value * rebateRate.Value / 100,
				}
				if err = w.doUserWash(userWashBet.ID, uw); err != nil {
					return err
				}
				break
			}
		}
	}
	return nil
}

func (w *UserWashService) doUserWash(userWashBetId int64, uw *schemaUserWash.UserWash) error {
	return w.svcCtx.Db.Transaction(func(tx *gorm.DB) error {
		// 开启事务
		// 清空投注量
		if err := userwash.NewUserWashBetModel(w.ctx, tx).BetClear(userWashBetId); err != nil {
			return err
		}
		// 增加洗码记录
		if err := userwash.NewUserWashModel(w.ctx, tx).AddUserWash(uw); err != nil {
			return err
		}
		// // 加余额
		// userWallet, err := user.NewWalletModel(w.ctx, tx).AddBalance(uw.UserId, uw.Value, &schemaUser.UserWallet{})
		// 钱包日志
		// _, err = user.NewWalletLogModel(w.ctx, tx).Create(
		//	userWallet, schemaUser.TRANSFER_TYPE_IN, uw.Value,
		//	schemaConfig.TYPE_BET_REBATE, strconv.FormatInt(uw.ID, 10), "", 0)
		// if err != nil {
		//	return err
		// }
		rb := &rpc.BalanceIncreaseRequest{
			UserId:      uw.UserId,
			Amount:      uw.Value,
			OperatorId:  0,
			TypeId:      config.TYPE_BET_REBATE,
			SourceId:    strconv.FormatInt(uw.ID, 10),
			Description: "bet rebate",
			ValidMark:   0,
		}
		_, err := BalanceIncrease(w.ctx, w.svcCtx, rb, nil)
		if err != nil {
			return err
		}

		return nil
	})
}
